package com.sky.loveshop.job;

import cn.hutool.core.collection.CollectionUtil;
import com.sky.loveshop.entity.Shop;
import com.sky.loveshop.service.MtService;
import com.sky.loveshop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xcy
 * @version V1.0
 * @description 从外送帮拉取美团订单定时任务
 * @date 2024/9/11 23:15
 */
@Slf4j
@Service
public class MeiTuanOrderJob {

    @Autowired
    private MtService mtService;
    @Autowired
    private ShopService shopService;

    @Scheduled(fixedRate = 120000)   // 每 2 分钟拉取一次
    public void pullMTOrderFromWSB() {
        List<Integer> allStoreIds = getAllStoreIds();
        for (Integer id : allStoreIds) {
            mtService.getMtOrderAndInsert(id);
        }
    }

    /**
     * 获取所有商店storeId
     */
    List<Integer> getAllStoreIds() {
        List<Shop> allShops = shopService.getAllShops();
        List<Integer> storeIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allShops)) {
            storeIds = allShops.stream()
                    .filter(s -> s.getStoreId() != null)
                    .map(Shop::getStoreId)
                    .collect(Collectors.toList());
        }
        return storeIds;
    }
}
